import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import layout from "@/layouts/baseLayout2.vue"
import Storage from "@/utils/storage"
import routerConfig from "@/layouts/config"

NProgress.configure({showSpinner: false});

const routes = [
  {
    path: '/',
    name: 'home',
    component: layout,
    redirect:'/loading',
    children: [
      {
        path: '/loading',
        name: 'loading',
        component: () => import( "@/views/Loading.vue" )
      },
      {
        path: '/overview',
        name: 'overview',
        component: () => import( "@/views/overview" )
      }, {
        path: '/archive',
        name: 'archive',
        component: () => import( "@/views/archiveData" )
      }, {
        path: '/alarm',
        name: 'alarm',
        component: () => import( "@/views/alarmManagement" )
      }, {
        path: '/hospitalDetail',
        name: 'hospitalDetail',
        component: () => import( "@/views/archiveData/hospitalDetail.vue" )
      }, {
        path: '/account',
        name: 'account',
        component: () => import( "@/views/user/account.vue" )
      },  {
        path: '/organization',
        name: 'organization',
        component: () => import( "@/views/archiveData/hospitalList.vue" )
      }, {
        path: '/404',
        name: '404',
        component: () => import( "@/views/404.vue" )
      }
    ]
  }, {
    path: '/login',
    name: 'login',
    component: () => import( "@/views/user/login.vue" )
  }, 
  {
    path: '/demo',
    name: 'demo',
    component: () => import( "@/views/demo.vue" )
  }
]


const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

router.beforeEach( (to, from, next) => {
  NProgress.start();
  if (routerConfig.menu.some(i => i.path == to.path)) {
    let headerItem = routerConfig.menu.find(i => i.path == to.path);
    Storage.setLocal("header_active", headerItem.id)
  }
  if(!to.name){
    router.push('/404')
  }
  next();
})

router.afterEach(() => {
  NProgress.done(); // finish progress bar
});

export default router
